package com.banzhi.shop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by HT on 2017/5/9.
 */

public abstract class CommonHeadAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_HEADER = 1;
    private static final int TYPE_FOOTER = 2;
    protected Context mContext;
    protected int mLayoutId;
    protected int eadLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;

    public CommonHeadAdapter(Context context, int layoutId, List<T> datas) {
        this(context, layoutId, 0, datas);
    }

    public CommonHeadAdapter(Context context, int layoutId, int HeadLayoutId, List<T> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        eadLayoutId = HeadLayoutId;
        mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = mInflater.inflate(eadLayoutId, parent, false);
            return new HeaderViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = mInflater.inflate(R.layout.item_footer_head, parent, false);
            return new FooterViewHolder(view);
        } else {
            View view = mInflater.inflate(mLayoutId, parent, false);
            return new ViewHolder(view);
        }

    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (eadLayoutId != 0) {
            if (position == 0) {
                headconvert((HeaderViewHolder) holder);
            } else if (position < mDatas.size() + 1) {
                convert((ViewHolder) holder, mDatas.get(position - 1), position);
                if (mOnItemClickListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = holder.getLayoutPosition(); // 1
                            mOnItemClickListener.onItemClick(holder.itemView, position); // 2
                        }
                    });

                }
            }
        } else {
            if (position < mDatas.size()) {
                convert((ViewHolder) holder, mDatas.get(position ), position);
                if (mOnItemClickListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = holder.getLayoutPosition(); // 1
                            mOnItemClickListener.onItemClick(holder.itemView, position); // 2
                        }
                    });

                }
            }
        }
    }
    //    摆放数据只能在用的时候才写

    public abstract void convert(ViewHolder holder, T t, int position);

    //    摆放数据只能在用的时候才写
    public abstract void headconvert(HeaderViewHolder holder);

    @Override
    public int getItemCount() {

        if (eadLayoutId == 0) {
            if(mDatas.size()==0){
                return mDatas.size();
            }else{
                return mDatas.size() + 1;
            }
        } else {
            if(mDatas.size()==0){
                return mDatas.size()+1;
            }else{
                return mDatas.size() + 2;
            }
        }
    }

    //是底部iewType为TYPE_FOOTER，不是底部viewType为TYPE_ITEM
    @Override
    public int getItemViewType(int position) {
        if (eadLayoutId != 0) {
            if (position == 0) {
                return TYPE_HEADER;
            } else if (position == getItemCount() - 1) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        } else {
            if (position == getItemCount() - 1) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        }
    }

    //    底部的ViewHolder
    class FooterViewHolder extends RecyclerView.ViewHolder {
        public FooterViewHolder(View itemView) {
            super(itemView);

        }
    }

    //    头部的ViewHolder
    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        //    SparseArray类似HaspMap不过比HashMap节省空间，方法都差不多，id和控件对应
        private SparseArray<View> mViews;
        private View mConvertView;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            mViews = new SparseArray<View>();
            mConvertView = itemView;
        }

        //根据id和内容进行摆放
        public void setText(int viewId, String msg) {
            TextView textView = getView(viewId);
            textView.setText(msg);
        }

        /**
         * 通过viewId获取控件
         *
         * @param viewId
         * @return
         */
        public <T extends View> T getView(int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = mConvertView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        //    SparseArray类似HaspMap不过比HashMap节省空间，方法都差不多，id和控件对应
        private SparseArray<View> mViews;
        private View mConvertView;

        public ViewHolder(View itemView) {
            super(itemView);
            mViews = new SparseArray<View>();
            mConvertView = itemView;
        }

        //根据id和内容进行摆放
        public void setText(int viewId, String msg) {
            TextView textView = getView(viewId);
            textView.setText(msg);
        }

        public TextView getTextView(int viewId) {
            TextView textView = getView(viewId);
            return textView;
        }

        public ImageView getImageView(int viewId) {
            ImageView imageView = getView(viewId);
            return imageView;
        }

        //根据id和内容进行摆放
        public void setImageURI(int viewId, String uri) {
            ImageView imageView = getView(viewId);
            Picasso.with(MyApp.context).load(uri).into(imageView);
        }

        /**
         * 通过viewId获取控件
         *
         * @param viewId
         * @return
         */
        public <T extends View> T getView(int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = mConvertView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);

    }

    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }
}
